function TextPage(){
	this.t_pdPage = null;
	this.t_edRender = null;
	this.t_textInfoList = [];
}

TextPage.prototype.AttachPage = function(pdPage){
	this.t_pdPage = pdPage;
};

TextPage.prototype.ParseTextInfo = function(rotate){
	if(this.t_pdPage == null)return;
	
	var txtObjCount = this.t_pdPage.pdPageObjects.textObjects.length;
	if(txtObjCount > 0){
		this.InitCharDiv();
		for(var i = 0; i < txtObjCount; i++){
			var textObj = this.t_pdPage.pdPageObjects.textObjects[i];
			if(textObj == null)continue;
			var textInfo = this.TextObjCharRect(textObj,rotate);
			if(textInfo != null)this.t_textInfoList.push(textInfo);
		}
		
		this.ExistLineFeed();
	}
};

TextPage.prototype.TextObjCharRect = function(textObj,rotate){
	var textInfo = new TextInfo();
	textInfo.setTextObj(textObj);
	var txtObjContent = textObj.content;
	var txtObjLen = txtObjContent.length;
	var txtObjRect = textObj.GetOriginPoint();
	var txtObjPoint = this.t_edRender.matrix.TransFormPoint(txtObjRect[0],txtObjRect[1]);
	textInfo.setCharRect(0,txtObjPoint);
	for(var i = 0; i < txtObjLen; i++){
		var txtState = this.t_pdPage.GetTextStateByID(textObj.nTextStateID);
		var fontInfo = this.t_pdPage.pdDoc.GetFontInfoByID(txtState.nFontID);
		if(fontInfo == null){
			textInfo = null;
			break;
		}
		var tgtMtx = PDMatrix.prototype.Concat2mt(textObj.matrix, this.t_edRender.matrix);
		var fontSize = tgtMtx.TransformYDistance(fontInfo.fontSize);
		var rect = this.CharRect(txtObjContent.charAt(i),fontInfo.fontFamily,fontSize);
		var charInfo = {'width':rect[0],'height':rect[1],'char':txtObjContent.charAt(i)};
		
			if(textObj.bVertWriting == 0){
				switch(rotate){
					case 0:if(i > 0){txtObjPoint = [Math.round(txtObjPoint[0]+rect[0]+1),Math.round(txtObjPoint[1])];}
							else{txtObjPoint = [Math.round(txtObjPoint[0]+1),Math.round(txtObjPoint[1]-rect[1])];};
							break;
					case 1:if(i > 0){txtObjPoint = [Math.round(txtObjPoint[0]+rect[1]+1),Math.round(txtObjPoint[1])];}
						   else {txtObjPoint = [Math.round(txtObjPoint[0]+1),Math.round(txtObjPoint[1])];};
						   charInfo = {'width':rect[1],'height':rect[0],'char':txtObjContent.charAt(i)};
						   break;
				}
			}
		
		textInfo.setCharRect(i,txtObjPoint);
		textInfo.setCharInfo(charInfo,i);
	}
	return textInfo;
};

TextPage.prototype.ExistLineFeed = function(){
	if(this.t_textInfoList.length == 0)return;
	var lineIndex = 0;
	for(var i = 0; i < this.t_textInfoList.length;i++){
		if(i < this.t_textInfoList.length - 1){
			var textObj = this.t_textInfoList[i].textObj;
			var this_nextCharRect = this.t_textInfoList[i+1].charRect[0];
			var x = this_nextCharRect[0];
			var y = this_nextCharRect[1];
			var this_textCharInfo = this.t_textInfoList[i].charInfo[0];
			var this_width = Math.abs(this_textCharInfo.width);
			var next_textCharInfo = this.t_textInfoList[i+1].charInfo;
			var next_textlstCharInfo = next_textCharInfo[next_textCharInfo.length - 1];
			var next_width = Math.abs(next_textlstCharInfo.width);
			var threshold = this_width > next_width ? this_width/4 : next_width/4;
			if (threshold < 1 )	threshold *= 2;
			if(textObj.bVertWriting == 0){
				if(Math.abs(y) > threshold) lineIndex++;
			}else{
				if(Math.abs(x) > threshold)lineIndex++;
			}
		}
		this.t_textInfoList[i].lineIndex = lineIndex;
		this.t_textInfoList[i].textObjIndex = i;
		var lastRect = [this.t_textInfoList[i].charRect[this.t_textInfoList[i].charRect.length - 1][0] + this.t_textInfoList[i].charInfo[this.t_textInfoList[i].charRect.length - 1].width,this.t_textInfoList[i].charRect[this.t_textInfoList[i].charRect.length - 1][1]];
		this.t_textInfoList[i].textObjRect = [this.t_textInfoList[i].charRect[0],lastRect,[this.t_textInfoList[i].charRect[0][1],this.t_textInfoList[i].charRect[0][1]+this.t_textInfoList[i].charInfo[0].height]];
	}
};

TextPage.prototype.InitCharDiv = function(){
	var charDiv = document.getElementById("charDiv");
	$(charDiv).remove();
	var bodyObj = document.getElementsByTagName("body")[0];
	var charDiv = document.createElement("div");
	charDiv.style.cssFloat = "left";
	charDiv.style.display = "none";
	charDiv.id = "charDiv";
	charDiv.style.textAlign = "center";
	charDiv.style.position = "relative";
	bodyObj.appendChild(charDiv);	
};

TextPage.prototype.CharRect = function(text,fontFamily,fontSize){
	var charDiv = document.getElementById("charDiv");
	charDiv.style.fontFamily = fontFamily;
	charDiv.style.fontSize = Math.round(fontSize) + "pt";
	charDiv.style.textAlign = "center";
	charDiv.innerHTML = text == " "?"&nbsp;":text;
	return [charDiv.offsetWidth,charDiv.offsetHeight];
};

TextPage.prototype.GetPDFRect = function (selectObj) {
    var rect_array = [];
    if (selectObj.nStartIndex == -1)
        return rect_array;
    var ObjStartY = this.t_textInfoList[selectObj.nStartObjIndex].charRect[0][1];
    var ObjEndY = this.t_textInfoList[selectObj.nEndObjIndex].charRect[0][1];
    //console.log("ObjStartY:"+ObjStartY+"ObjEndY:"+ObjEndY);
    var objSize = this.t_textInfoList.length;
    for (var i = 0; i < objSize; i++) {
        var textObj = this.t_textInfoList[i];
        if((selectObj.nStartObjIndex <= selectObj.nEndObjIndex && i > selectObj.nStartObjIndex && i < selectObj.nEndObjIndex) || i == selectObj.nStartObjIndex || i == selectObj.nEndObjIndex){
        	var startIndex = selectObj.nStartObjIndex == i?selectObj.nStartIndex:0;
        	var endIndex = selectObj.nEndObjIndex == i?selectObj.nEndIndex:textObj.GetSize();
        	//console.log("str:"+startIndex+"end:"+endIndex);
        	rect_array.push(textObj.GetRectByIndex(startIndex,endIndex));
        }
    }
    return rect_array; 
};


    
TextPage.prototype.GetSize = function () 
{
    var size = this.t_textInfoList.length;
    if (size == 0) {
        return 0;
    }
    else {
        return this.t_textInfoList[size - 1].GetSize();
    }
};